import { createSlice, PayloadAction } from '@reduxjs/toolkit'
export interface IUserState {
  loginState: boolean
  adminname: string
  token: string
  role: number
  checkedkeys: any[]
}
const initialState: IUserState = {
  loginState: localStorage.getItem('loginState') === 'true',
  adminname: localStorage.getItem('name') || '',
  token: localStorage.getItem('token') || '',
  role: Number(localStorage.getItem('role')),
  checkedkeys: JSON.parse(localStorage.getItem('checkedKeys')!)
}

export const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    changeLoginState(state, action: PayloadAction<boolean>) {
      state.loginState = action.payload
    },
    changeName(state, action: PayloadAction<string>) {
      state.adminname = action.payload
    },
    changeToken(state, action: PayloadAction<string>) {
      state.token = action.payload
    },
    changeRole(state, action: PayloadAction<number>) {
      state.role = action.payload
    },
    changeCheckedkeys(state, action: PayloadAction<any[]>) {
      state.checkedkeys = action.payload
    }
  }
})
export const { changeLoginState, changeName, changeToken, changeRole, changeCheckedkeys } = userSlice.actions
export default userSlice.reducer